package com.yy.smartBI.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yy.smartBI.bizmq.MyMessageProducer;
import com.yy.smartBI.common.BaseResponse;
import com.yy.smartBI.mapper.GenChartMapper;
import com.yy.smartBI.model.dto.chart.UploadExcelToCsvRequest;
import com.yy.smartBI.model.entity.GenChart;
import com.yy.smartBI.model.entity.User;
import com.yy.smartBI.service.ChartService;
import com.yy.smartBI.utils.AIUtils;
import com.yy.smartBI.utils.ExcelUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

import static com.yy.smartBI.constant.UserConstant.USER_LOGIN_STATE;


@Service
@Slf4j
public class ChartServiceImpl implements ChartService {

    @Resource
    private GenChartMapper genChartMapper;
    @Resource
    private ThreadPoolExecutor threadPoolExecutor;
    @Resource
    private MyMessageProducer myMessageProducer;

    @Override
    public boolean uploadExcelToChartsAsynMQ(MultipartFile file, UploadExcelToCsvRequest uploadExcelToCsvRequest, HttpServletRequest request) {
        String chartType = uploadExcelToCsvRequest.getChartType();
        //2.使用EasyExcel读文件
        String data;
        try {
            data = ExcelUtils.excelToCsv(file);
        } catch (Exception e) {
            log.error("excel 转 csv 失败", e);
            return false;
        }
        if (StrUtil.isBlank(data)) {
            log.error("excel 转 csv 无结果");
            return false;
        }

        //3.调用大模型
        //创建图表
        String question = uploadExcelToCsvRequest.getChartType() + data;
        //创建图表
        GenChart chart = new GenChart();
        chart.setChartType(chartType);
        User currentUser = (User) request.getSession().getAttribute(USER_LOGIN_STATE);
        if (currentUser != null) {
            chart.setUserId(currentUser.getId());
        }
        chart.setStatus("wait");
        //异步调用
        try {
            CompletableFuture.runAsync(() -> {
                //1.先保存任务到数据库
                int insert = genChartMapper.insert(chart);
                QueryWrapper<GenChart> genChartQueryWrapper = new QueryWrapper<>();
                genChartQueryWrapper.eq("id", chart.getId());
                if (insert < 1) {
                    log.error("数据库执行出错 创建图表失败");
                    throw new IllegalArgumentException("数据库执行出错 创建图表失败");
                }
                //2.使用消息队列 调用大模型
                String message = question+"]]]]]"+chart.getId();
                myMessageProducer.sendMessage("code_exchange", "my_routingKey",message);
            }, threadPoolExecutor);
        } catch (Exception e) {
            log.error("异步调用失败", e);
            return false;
        }
        return true;
    }


}
